package com.yc.atm;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.log4j.Logger;

import java.io.PrintWriter;
import java.lang.reflect.Type;
import java.net.Socket;
import java.util.Scanner;

public class Atmclient {
    private static Logger log=Logger.getLogger(Atmclient.class.getName());

    public static void main(String[] args) {
        String host ="localhost";
        int port=12000;
        //创建一个socket
        Scanner keyboard=new Scanner(System.in);//键盘
        boolean flag=true;
        try (
                Socket s=new Socket(host,port);
                PrintWriter pw=new PrintWriter(s.getOutputStream());//println()可以以一行一行形式发送数据
                Scanner sc=new Scanner(s.getInputStream())//读取服务器的响应 nextLine()
                ){
            log.info("连接ATM服务"+s.getRemoteSocketAddress()+"成功");
            do{
                System.out.println("======中国银行ATM欢迎您======");
                System.out.println("1.存");
                System.out.println("2.取");
                System.out.println("3.查询");
                System.out.println("4.退出");
                System.out.println("5.错误指令（没有指令）");
                System.out.println("==================");
                System.out.println("请输入你的操作选项");
                String command=keyboard.nextLine();
                String response=null;
                if ("1".equalsIgnoreCase(command)){
                    //TODO；操作金额输入
                    pw.println("DEPOSIT 1 100");
                }else if ("2".equalsIgnoreCase(command)){
                    pw.println("WITHDRAW 1 10");
                }else if ("3".equalsIgnoreCase(command)){
                    pw.println("BALANCE 1");
                }else if ("5".equalsIgnoreCase(command)){
                    pw.println("adfdsf");
                }else {
                    pw.println("QUIT");
                    pw.flush();
                    flag=false;
                    break;
                }
                pw.flush();
                //取服务器的响应
                response=sc.nextLine();
                System.out.println("服务器的响应"+response);
                Gson gson=new Gson();
                //设定gson处理字符串数据的模版。 vue axios
                Type type=new TypeToken<JsonModel<BankAccount>  >(){}.getType();
                JsonModel<BankAccount>jm=gson.fromJson(response,type);
                if (jm.getCode()==1){
                    BankAccount ba=jm.getObj();
                    System.out.println(ba.getId()+"\t"+ ba.getBalance());
                }else {
                    System.out.println(jm.getErrorMsg());
                }
            }while (flag);
            System.out.println("ATM机退出当前账号，程序结束");

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
